﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Permissions;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FTI
{
    static class Program
    {
        /// <summary>
        /// Get the composition container. This class facilitates the ability to dynamically load extensions at runtime.
        /// </summary>
        public static Composition Container { get; private set; }

        public static List<string> Arguments { get; set; }

        public static bool IsSetupProcess
        {
            get
            {
                return Arguments.Contains("-setup");
            }
        }

        public static bool IsProcessElevated
        {
            get
            {
                WindowsIdentity
                    oIdentity = WindowsIdentity.GetCurrent();
                WindowsPrincipal
                    oPrincipal = new WindowsPrincipal(oIdentity);

                return oPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
            }
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(params string[] arguments)
        {
            Arguments = new List<string>();

            if(arguments.Length > 0)
            {
                Arguments.AddRange(arguments);
            }

            if (!IsSetupProcess)
            {
                Container = new Composition();

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
            else if (IsProcessElevated)
            {   // we need to configure some stuff so the application can run properly
                if (EventLog.SourceExists(FTI.Logging.EventLogging.source))
                {   // Clean it up we may have changed somthing
                    EventLog.DeleteEventSource(FTI.Logging.EventLogging.source, ".");
                }
                // Create the Event source so we can log errors that the application may encounter
                EventLog.CreateEventSource(new EventSourceCreationData(FTI.Logging.EventLogging.source, String.Format("{0} Event Log", FTI.Logging.EventLogging.source))
                {
                    MachineName = "."
                });

                RegistryKey hklm = Registry.LocalMachine;
                string
                    keyName = String.Format(@"SYSTEM\CurrentControlSet\Services\EventLog\{0} Event Log\{0}", FTI.Logging.EventLogging.source),
                    file = "";
                RegistryPermission 
                    perm = new RegistryPermission(RegistryPermissionAccess.Create | RegistryPermissionAccess.Write, @"HKEY_LOCAL_MACHINE\" + keyName);
                RegistryKey el = hklm.OpenSubKey(keyName, true);

                file = Environment.CurrentDirectory + @"\FTI.Logging.Messages.dll";

                try
                {
                    el.SetValue("CategoryMessageFile", file);
                    el.SetValue("CategoryCount", Enum.GetNames(typeof(FTI.SystemCategory)).Length);
                }
                catch (Exception) { }
            }
        }

        
    }
}
